package servlets;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.regex.Pattern;

@WebServlet(name = "JudgeServlet", value = "/judge")
public class JudgeServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //解决中文乱码问题
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        //获取前端传回的信息
        String info = request.getParameter("inputInfo");
        //进行信息处理和逻辑判断

        String regEx = "[^0-9]";
        Pattern pattern = Pattern.compile(regEx);
        String[] result = pattern.split(info);
        if (result.length == 0) {
            response.getWriter().write("字符串中没有数字！");
        } else {
            for (String e : result) {
                if (e.length() > 0) {
                    response.getWriter().write(e + "\n");
                }
            }
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
